/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package epucjr.psicopuc.view.componentes;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;



public class BotaoNaColuna extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener {

    /**
	 * 
	 */
	private   int x = 0;
	private static final long serialVersionUID = -7509307768672529678L;
	JTable table;
    JButton renderButton;
    JButton editButton;
    private String text;
    ImageIcon icon;
    private String valor;
    private TableColumnModel columnModel;

    /** Creates a new instance of BotaoNaColuna */
    public BotaoNaColuna(JTable table, int column, String t) {
        super();
        this.table = table;
        //this.tipo = t;
        this.setText("");
        this.renderButton = new JButton();
        this.setValor(t);

        editButton = new JButton();
        editButton.setFocusPainted(false);
        editButton.addActionListener(this);

        this.columnModel = table.getColumnModel();
        columnModel.getColumn(column).setCellRenderer(this);
        columnModel.getColumn(column).setCellEditor(this);

    }

    public Component getTableCellRendererComponent(
    		
            JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (hasFocus) {
            renderButton.setForeground(table.getForeground());
            renderButton.setBackground(UIManager.getColor("Button.background"));
        } else if (isSelected) {
            renderButton.setForeground(table.getSelectionForeground());
            renderButton.setBackground(table.getSelectionBackground());
        } else {
            renderButton.setForeground(table.getForeground());
            renderButton.setBackground(UIManager.getColor("Button.background"));
        }

        if (value instanceof ImageIcon) {
            renderButton.setIcon((value == null) ? null : (ImageIcon) value);
        }/* else if (value instanceof String) {
            renderButton.setText((value == null) ? "" : value.toString());
        }*/
        return renderButton;
    }

    public Component getTableCellEditorComponent(
            JTable table, Object value, boolean isSelected, int row, int column) {

        if (value instanceof ImageIcon) {
            icon = (value == null) ? null : (ImageIcon) value;
            editButton.setIcon(icon);

        } /*else if (value instanceof String) {
            text = (value == null) ? "" : value.toString();
            editButton.setText(text);
        }*/

        return editButton;
    }

    public Object getCellEditorValue() {
        return text;
    }


        public void actionPerformed(ActionEvent e) {

        

         System.out.println("Linha = " + table.getValueAt(this.getTable().getSelectedRow(), 1));
        // System.out.println("Coluna = " + table.getColumnCount());
//        String prefixoExecuta = "rundll32.exe Shell32.dll,ShellExec_RunDLL";
//        String pathArquivos = "";
//
//        if (getTipo() == 0) {
//            pathArquivos = Constantes.getPATH_ARQUIVOS();
//        } else {
//            pathArquivos = Constantes.getPATH_IMAGENS();
//        }
//
//        fireEditingStopped();
//        Utils.ExecutaComando.executa(prefixoExecuta + " \"" + pathArquivos + "\\" + e.getActionCommand() + "\"");
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getValor() {
        return valor;
    }

    public void setValor(String valor) {
        this.valor = valor;
    }

    public JTable getTable() {
        return table;
    }
}
